package com.imc.platform.base.pay.http.tuple;

import com.imc.core.utils.MD5Utils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;

/**
 * Created by tanweiping on 2018/9/4.
 */
@XmlRootElement
@Builder
@Data@AllArgsConstructor@NoArgsConstructor
public class PrePayParams {

    @XmlTransient
    private String appid;
    @Builder.Default
    @XmlTransient
    private String attach = "支付测试";
    @Builder.Default
    @XmlTransient
    private String body = "JSAPI支付测试";
    @XmlTransient
    private String mch_id;
    @XmlTransient
    private String detail;
    @XmlTransient
    private String nonce_str;
    @XmlTransient
    @Builder.Default
    private String notify_url = "https://www.nvebo.com/manage/api/expose/wx/notify";
    @XmlTransient
    private String openid;
    @XmlTransient
    private String out_trade_no;
    @XmlTransient
    private String spbill_create_ip;
    @XmlTransient
    @Builder.Default
    private int total_fee=1;
    @XmlTransient
    @Builder.Default
    private String trade_type = "JSAPI";
    @XmlTransient
    private String sign;


    public String getNonce_str() {
        if (StringUtils.isEmpty(nonce_str)){
            nonce_str = UUID.randomUUID().toString().substring(0,16);
        }
        return nonce_str;
    }

    public String getMySign(String key) {
        String stringA="appid="+getAppid()+"&attach="+getAttach()
                +"&body="+getBody()
                +"&detail="+getDetail()
                +"&mch_id="+getMch_id()
                +"&nonce_str="+getNonce_str()
                +"&notify_url="+getNotify_url()
                +"&openid="+getOpenid()
                +"&out_trade_no="+getOut_trade_no()
                +"&spbill_create_ip="+getSpbill_create_ip()
                +"&total_fee="+getTotal_fee()
                +"&trade_type="+getTrade_type();
        String stringSignTemp=stringA+"&key="+key; //注：key为商户平台设置的密钥key
        return MD5Utils.MD5Encode(stringSignTemp,"UTF-8").toUpperCase(); //注：MD5签名方式
    }
}
